import Koa from 'koa'
import {Reply} from './reply/Reply'
import cors from 'koa-cors'
const json = require('koa-json')
const bodyparser = require('koa-bodyparser')
const koaBody = require('koa-body');
const mongoose=require('mongoose')
const views = require('koa-views');

const config=require('./database/config')
const myLoger = require('./logs/index')



//路由表
const huarenjie = require('./api/huarenjie')
const test=require('./api/test')
const admin=require('./api/admin')
const users=require('./api/users')
const msgs=require('./api/msgs')
const uploads=require('./api/uploads')
const groups=require('./api/groups')
const count=require('./api/count')


//连接数据库
mongoose.connect(config.url,function (err) {
    if(err){
        console.log('mongodb connect fail')
    }
    else{
        console.log('mongodb has connected')

    }
})





const app = new Koa()


app.use(cors())


app.use(myLoger())

app.use(koaBody({multipart: true}));

app.use(bodyparser({
    enableTypes:['json', 'form', 'text']
}))
app.use(json())

app.use(views(__dirname + '/views', {
    extension: 'ejs'
}))
app.use(require('koa-static')(__dirname + '/public'))


app.use(async (ctx,next) => {
    ctx.set('Access-Control-Allow-Credentials', true);
    await next()
})



app.use(huarenjie.routes(), huarenjie.allowedMethods())
app.use(test.routes(), test.allowedMethods())
app.use(admin.routes(), admin.allowedMethods())
app.use(users.routes(), users.allowedMethods())
app.use(msgs.routes(), msgs.allowedMethods())
app.use(uploads.routes(), uploads.allowedMethods())
app.use(groups.routes(), groups.allowedMethods())
app.use(count.routes(), count.allowedMethods())


var server = require('http').Server(app.callback())
var io = require('socket.io')(server);


io.attach(server, {
    pingInterval: 30000,
    pingTimeout: 10000,
    cookie: false,
    // transports: ['polling', 'websocket']
});


io.on('connection',(socket)=>{              //监听客户端的连接事件
    Reply(socket,io)
});


server.listen(3001)
console.log('系统启动，端口：3001')

